<?php
/**
 * Created by PhpStorm.
 * User: kcswag
 * Date: 12/3/17
 * Time: 2:42 PM
 */

namespace AppBundle\Controller;

use AppBundle\Entity\P2pBidding;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Libs\Publisher;
use AppBundle\Libs\Storage;
use Symfony\Component\HttpFoundation\Response;

/**
 * Class PublishController
 * @package AppBundle\Controller
 * @Route("/installment/publish")
 */
class PublishController extends Controller
{
    private $publisher;
    private $storage;

    public function __construct(Publisher $publisher, Storage $storage)
    {
        $this->publisher = $publisher;
        $this->storage = $storage;
    }


    /**
     * @Route("/notification")
     */
    public function receiveProjectNotificationApi(Request $request){
        //store project data
        $project = $this->storeProjectData($request);

        //store borrower data
        $borrower = $this->storeBorrowerData($request);

        $this->publishProject($project);
    }


    protected function storeProjectData(Request $request){
        $data = [];

        return $this->storeBorrowerData($data);

    }


    protected function storeBorrowerData(Request $request){
        $data = [];
        $data['name'] = $request->get('name');
        $data['cellphone'] = $request->get('cellphone');
        $data['address'] = $request->get('address');
        $data['income'] = $request->get('income');

        return $this->storage->storeBorrowerData($data);
    }


    protected function publishProject(P2pBidding $project){
        $this->publisher->publish($project);
    }

}